let setCookie = function (name, value) {
    var argv = arguments;
    var argc = argv.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = escape(name) + "=" + escape(value) + "; expires=" +
        ((expires == null) ? exp.toGMTString() : expires.toGMTString()) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
};

let getCookieVal = function (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
};

let getCookie = function (name) {
    var arg = escape(name) + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return "";
};

let deleteCookie = function (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
};

let setItem = function (name, obj) {
    var val = typeof obj == 'object' ? JSON.stringify(obj) : obj;
    if (window.localStorage) {
        window.localStorage.setItem(name, val);
    } else {
        setCookie(name, val);
    }
};

let getItem = function (name) {
    var re = /^[{]{1}[\S]*[}]{1}$/;
    if (window.localStorage) {
        var result = window.localStorage.getItem(name);
        if (re.test(result)) {
            return JSON.parse(result);
        } else {
            return result;
        }
    } else {
        var result = getCookie(name);
        if (re.test(result)) {
            return JSON.parse(result);
        } else {
            return result;
        }
    }
};

let removeItem = function (name) {
    if (window.localStorage) {
        window.localStorage.removeItem(name);
    } else {
        deleteCookie(name);
    }
};
export {
  setCookie,
  getCookie,
  deleteCookie,
  setItem,
  getItem,
  removeItem
}